﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;

namespace RN.ERP.Web.PersonChange
{
    public partial class CorpChangeRYSelect : BasePage
    {
        public override bool IsCheckPermission
        {
            get { return false; }
        }
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdnUserGuid.Value = UserInfo.CorpUserGuid;
            }
        }

        protected void ListPersons_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }
        private string GetCondition()
        {
            List<QueryPersonInfoCondition> conditions = new List<QueryPersonInfoCondition>();
            QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
            string name = txtName.Text.Trim();
            if (!string.IsNullOrEmpty(name))
            {
                condition.Name = new ParameterValue<string>() { O = OptEnum.Like, V = name };
            }
            condition.CorpGuid = new ParameterValue<Guid>() { O = OptEnum.Equal, V = Guid.Parse(hdnUserGuid.Value) };
            conditions.Add(condition);
            return JsonConvert.SerializeObject(conditions);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ListPersons.CurrentPageIndex = 0;
            BindData();
            ListPersons.DataBind();
        }

        private void BindData()
        {
            ListPersons.AllowCustomPaging = true;
            var queryCondition = GetCondition();
            var cnt = basicServiceClient.GetChangeEmpCount(queryCondition);
            var corp = basicServiceClient.GetChangeEmpList(queryCondition, ListPersons.CurrentPageIndex, ListPersons.PageSize);
            var corpInfo = JsonConvert.DeserializeObject<IList<tb_Emp_Manager>>(corp);
            ListPersons.VirtualItemCount = cnt;
            ListPersons.DataSource = corpInfo;
        }
    }
}